import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '../views/home/HomeView.vue'
import useUserStore from '@/stores/user'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    //首页
    {
      path: '/home',
      name: 'home',
      component: HomeView,
      redirect: '/home/portal',
      meta: {
        title: 'home'
      },
      children: [
        {
          path: '/home/portal',
          name: 'portal',
          component: () => import('../views/portal/index.vue'),
          meta: {
            title: '首页'
          }
        }
      ]
    },
    {
      path: '/echart',
      name: 'echart',
      component: () => import('../views/echart/index.vue')
    },
    {
      path: '/contol',
      name: 'contol',
      redirect: '/contol/user',
      component: () => import('../views/home/HomeView.vue'),
      meta: {
        title: '权限管理'
      },
      children: [
        {
          path: '/contol/menu',
          name: 'menu',
          meta: {
            title: '菜单管理'
          },
          component: () => import('../views/contol/menu/index.vue')
        },
        {
          path: '/contol/role',
          name: 'role',
          meta: {
            title: '角色管理'
          },
          component: () => import('../views/contol/role/index.vue')
        },
        {
          path: '/contol/user',
          name: 'user',
          meta: {
            title: '用户管理'
          },
          component: () => import('../views/contol/user/index.vue')
        }
      ]
    },
    {
      path: '/goods',
      name: 'goods',
      redirect: '/goods/brand',
      meta: {
        title: '商品管理'
      },
      component: () => import('../views/home/HomeView.vue'),
      children: [
        {
          path: '/goods/attribute',
          name: 'attribute',
          meta: {
            title: '属性管理'
          },
          component: () => import('../views/goods/attribute/index.vue')
        },
        {
          path: '/goods/brand',
          name: 'brand',
          meta: {
            title: '品牌管理'
          },
          component: () => import('../views/goods/brand/index.vue')
        },
        {
          path: '/goods/sku',
          name: 'sku',
          meta: {
            title: 'sku管理'
          },
          component: () => import('../views/goods/sku/index.vue')
        },
        {
          path: '/goods/spu',
          name: 'spu',
          meta: {
            title: 'spu管理'
          },
          component: () => import('../views/goods/spu/index.vue')
        }
      ]
    },
    //登录页面
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login/index.vue')
    },
    //404页面
    {
      path: '/:pathMatch(.*)*',
      name: '404',
      component: () => import('../views/404/index.vue')
    }
  ]
})
router.beforeEach((to, from, next) => {
  //进度条
  NProgress.start()
  //校验登录
  const userstore = useUserStore()
  const token = userstore.token
  if (to.path === '/login') {
    if (token) {
      next('/home')
    } else {
      next()
    }
  } else {
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
  //路由鉴权
})

router.afterEach(() => {
  NProgress.done()
})
export default router
